#include "keyboard.h"
#include "key.h"
#include <qevent.h>
#include <qapplication.h>
#include <qdebug.h>
int CProxyStyle::styleHint(StyleHint hint, const QStyleOption *option, const QWidget *widget, QStyleHintReturn *returnData) const
{
    if (hint == QStyle::SH_RequestSoftwareInputPanel)
        return QStyle::RSIP_OnMouseClick;
    return QProxyStyle::styleHint(hint, option, widget, returnData);
}

CKeyBoard::CKeyBoard(QObject *parent) : QInputContext(parent)
{
    pKey = new CKey();
    connect(pKey, SIGNAL(characterGenerated(QChar)), SLOT(sendCharacter(QChar)));
    connect(&timer, SIGNAL(timeout()), this, SLOT(timeout()));
}

CKeyBoard::~CKeyBoard()
{
    delete pKey;
}

QString CKeyBoard::identifierName()
{
    return QString();
}

QString CKeyBoard::language()
{
    return QString();
}

void CKeyBoard::reset()
{

}

bool CKeyBoard::isComposing() const
{
    return true;
}

bool CKeyBoard::filterEvent(const QEvent* event)
{
    if (event->type() == QEvent::RequestSoftwareInputPanel)
    {
        updatePosition();
        startMyTimer(true);
        return true;
    }
    else if (event->type() == QEvent::CloseSoftwareInputPanel)
    {
        startMyTimer(false);
        emit txPopupKeyboard(false);
        return true;
    }
    return false;
}

void CKeyBoard::sendCharacter(QChar character)
{
    QWidget *pFocusWidget = QApplication::focusWidget();
    if (pFocusWidget != NULL)
    {
        if (character.toAscii() == '\b')
        {
            QKeyEvent keyPress(QEvent::KeyPress, Qt::Key_Backspace, Qt::NoModifier, QString());
            QCoreApplication::sendEvent(pFocusWidget, &keyPress);

            QKeyEvent keyRelease(QEvent::KeyRelease, Qt::Key_Backspace, Qt::NoModifier, QString());
            QCoreApplication::sendEvent(pFocusWidget, &keyRelease);
        }
        else if (character.toAscii() == '\r')
        {
            QKeyEvent keyPress(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier, QString());
            QCoreApplication::sendEvent(pFocusWidget, &keyPress);

            QKeyEvent keyRelease(QEvent::KeyRelease, Qt::Key_Enter, Qt::NoModifier, QString());
            QCoreApplication::sendEvent(pFocusWidget, &keyRelease);
            emit txPopupKeyboard(false);
            startMyTimer(false);
        }
        else
        {
            QKeyEvent keyPress(QEvent::KeyPress, character.unicode(), Qt::NoModifier, QString(character));
            QCoreApplication::sendEvent(pFocusWidget, &keyPress);

            QKeyEvent keyRelease(QEvent::KeyRelease, character.unicode(), Qt::NoModifier, QString());
            QCoreApplication::sendEvent(pFocusWidget, &keyRelease);
        }
    }
}

void CKeyBoard::updatePosition()
{
    QWidget *pFocusWidget = QApplication::focusWidget();
    if (pFocusWidget != NULL)
    {
//        QWidget *pTopWidget = pFocusWidget->parentWidget();
//        QWidget *pTmpWidget = pTopWidget;
//        while(pTmpWidget != NULL)
//        {
//            pTopWidget = pTmpWidget;
//            pTmpWidget = pTmpWidget->parentWidget();
//        }
//        if (pTopWidget == NULL)
//            pTopWidget = pFocusWidget;

        //panelStartPos = QPoint(0, WND_HEIGHT - 260);
//        panelStartPos = QPoint(pTopWidget->pos().x(),
//                               pTopWidget->pos().y() + pTopWidget->height());


        panelStartPos = QPoint(pFocusWidget->pos().x(),
                               pFocusWidget->pos().y());
        panelStartPos = pFocusWidget->mapToGlobal(panelStartPos);
        increment = 0;
    }
}

void CKeyBoard::timeout()
{
    int w = pKey->width();
    int h = pKey->height();
    int x = panelStartPos.x();
    increment += 10;
    int y = panelStartPos.y();

    if (increment >= h)
    {
        timer.stop();
        if (!bShow)
        {
            pKey->hide();
        }
    }
    else
    {
        if (bShow)
        {
           y = panelStartPos.y() + h - increment;
        }
        else
        {
           y = panelStartPos.y() + increment;
        }
    }

    Q_UNUSED(x)
    Q_UNUSED(y)

    pKey->setGeometry(pKey->lastPos.x(),pKey->lastPos.y(), w, h);
}

void CKeyBoard::startMyTimer(bool bShow)
{
    this->bShow = bShow;

    if (bShow)
        pKey->show();

    if (!timer.isActive())
    {
        increment = 0;
        timer.start(5);
    }
}

void CKeyBoard::showKeyboard()
{
    QWidget *pFocusWidget = qApp->focusWidget();
    if (pFocusWidget != NULL)
    {
        QEvent e(QEvent::RequestSoftwareInputPanel);
        QApplication::sendEvent(pFocusWidget, &e);
    }
}

void CKeyBoard::hideKeyboard()
{
    QWidget *pFocusWidget = qApp->focusWidget();
    if (pFocusWidget != NULL)
    {
        QEvent e(QEvent::CloseSoftwareInputPanel);
        QApplication::sendEvent(pFocusWidget, &e);
    }
}
